const initGeetest = require("./gt.js");
(function(global, factory) {
  "use strict";
  if (typeof module === "object" && typeof module.exports === "object") {
    // CommonJS
    module.exports = global.document
      ? factory(global, true)
      : function(w) {
          if (!w.document) {
            throw new Error("Geetest requires a window with a document");
          }
          return factory(w);
        };
  } else {
    factory(global);
  }
})(typeof window !== "undefined" ? window : this, function(window, noGlobal) {
  "use strict";
  if (typeof window === "undefined") {
    throw new Error("slider code requires browser environment");
  }
  var ins = {
    dom: {
      el: null,
      loading: null
    },
    timer: null,
    code: null,
    https: false,
    locale: null,
    init(el, loading, https = false, locale) {
      ins.destroy();
      this.dom.el = el;
      this.dom.loading = loading;
      this.https = https;
      this.locale = locale;
    },
    debounce(fn, time = 1000) {
      if (this.timer) {
        clearTimeout(this.timer);
      }
      this.timer = setTimeout(() => {
        fn();
        clearTimeout(this.timer);
      }, time);
    },
    onRegister(data) {
      // if (!!ins.captchaObj) {
      //   console.log('sdlkfjdskljfl');
      //   ins.captchaObj.destroy();
      // }
      // 调用 initGeetest 进行初始化
      // 参数1：配置参数
      // 参数2：回调，回调的第一个参数验证码对象，之后可以使用它调用相应的接口
      initGeetest(
        {
          // 以下 4 个配置参数为必须，不能缺少
          gt: data.gt,
          challenge: data.challenge,
          offline: !data.success, // 表示用户后台检测极验服务器是否宕机
          new_captcha: data.new_captcha, // 用于宕机时表示是新验证码的宕机
          product: "float", // 产品形式，包括：float，popup
          width: "100%",
          https: ins.https
          // 更多配置参数说明请参见：http://docs.geetest.com/install/client/web-front/
        },
        this.onReadyCode
      );
    },
    hideLoading() {
      this.dom.loading.style.display = "none";
    },
    showLoading() {
      this.dom.loading.style.display = "block";
    },
    destroy() {
      if (!!ins.code) {
        //避免内存泄漏
        document.querySelectorAll("script[src*=geetest]").forEach(node => {
          node.remove();
        });
        document.querySelectorAll("link[href*=geetest]").forEach(node => {
          node.remove();
        });
        // ins.code.reset();
        ins.code.destroy();
        ins.code = null;
      }
    },
    onReadyCode(code) {
      ins.code = code;
      // captchaObj.appendTo('#captcha'); // 同时插入三个input表单时，geetest_challenge, geetest_validate, geetest_seccode
      code.appendTo(ins.dom.el); // 同时插入三个input表单时，geetest_challenge, geetest_validate, geetest_seccode
      code.onReady(function() {
        ins.hideLoading();
        // ins.initLocale();
      });
      code.onSuccess(function() {
        var res = code.getValidate();
        ins.onSuccess(res);
      });
      // 更多接口说明请参见：http://docs.geetest.com/install/client/web-front/
    },
    onSuccess: function() {},
    initLocale() {
      switch (ins.locale) {
        case "en-US":
          setTimeout(function() {
            console.log(
              ins.dom.el.querySelector("span.geetest_radar_tip_content"),
              8888888
            );
          }, 2000);
          // ins.dom.el.querySelector("span.geetest_radar_tip_content").innerText =
          //   "Click button verification";
          // ins.dom.el.querySelector("span.geetest_reset_tip_content").innerText =
          //   "Click try again";
          break;
        case "zh-CN":
          ins.dom.el.querySelector("span.geetest_radar_tip_content").innerText =
            "点击按钮进行验证";
          ins.dom.el.querySelector("span.geetest_reset_tip_content").innerText =
            "请点击重试";
          break;
        case "de-DE":
          const timer = setTimeout(function() {
            ins.dom.el.querySelector(
              "span.geetest_radar_tip_content"
            ).innerText = "Click button verification";
            ins.dom.el.querySelector(
              "span.geetest_reset_tip_content"
            ).innerText = "Click try again";
            clearTimeout(timer);
          }, 0);
          break;
      }
    },
    getValidate() {
      if (!!this.code) {
        return this.code.getValidate();
      }
      return undefined;
    }
  };
  window.sliderCode = ins;
  return ins;
});
